//
//  EssencePictureController.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/19.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "EssencePictureController.h"
#import <AFNetworking.h>
#import "TupianTableViewCell.h"
#import "TupianModel.h"
#import "MJRefresh.h"
#import "CommentsViewController.h"
static NSString *const firstCell = @"firstCell";

@interface EssencePictureController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *allArray;
@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, assign)NSInteger nextPage;
@end

@implementation EssencePictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArray = [NSMutableArray array];
//    [self getData];
    // 设置背景色
    self.view.backgroundColor = DSRGBA(223, 223, 223, 1);
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 155) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView. dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView registerClass:[TupianTableViewCell class] forCellReuseIdentifier:firstCell];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.isUpLoading = NO;
    [self addFooter];
    [self addHeader];
    // 刚进界面就刷新
    [self.myTableView.mj_header beginRefreshing];
}
#pragma mark --下拉刷新
-(void)addHeader{
    //block中不能使用self,会报错
    __block EssencePictureController *recommendVC = self;
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //记录页码
        //recommendVC.nextPage = 0;
        self.nextPage = 0;
        //标记下拉刷新
        recommendVC.isUpLoading = NO;
        //请求数据
        [recommendVC getData:self.nextPage];
        
    }];
    
    
    self.myTableView.mj_header = header;
}

#pragma mark -- 上拉加载更多
-(void)addFooter{
    
    __block EssencePictureController *recommendVC = self;
    //添加上拉刷新尾部控件
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        //上拉一次自增一次
        //recommendVC.nextPage = 0;
        self.nextPage ++;
        //标记为上拉操作
        recommendVC.isUpLoading = YES;
        //请求数据
        [recommendVC getData:self.nextPage];
        
        
        
        
    }];
    
    //    [footer setImages:self.imageArray forState:MJRefreshStateRefreshing];
    self.myTableView.mj_footer = footer;
    
}
-(void)getData:(NSInteger)nextPage{
    NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/list/jingxuan/10/bs0315-iphone-4.1/%ld-20.json",nextPage * 1464543926];
    //GET请求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//                NSLog(@"%@",responseObject);
        NSMutableArray *listArray = [responseObject objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            TupianModel *tuModel = [[TupianModel alloc] init];
            [tuModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:tuModel];
        }
        [self.myTableView reloadData];
        //停止刷新
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //        NSLog(@"%@",error);
        
    }];
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TupianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstCell];
    cell.tuModel = [self.allArray objectAtIndex:indexPath.row];
    cell.selectionStyle = 0;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TupianModel *tuModel = [self.allArray objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    CGRect rect = [tuModel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    if ([tuModel.type isEqualToString:@"gif"]) {
        CGFloat gifHeight = tuModel.gifModel.height.intValue * 394 / tuModel.gifModel.width.intValue;
        CGFloat ImageHeight = gifHeight;
        return rect.size.height + ImageHeight + 160;
    }
    if ([tuModel.type isEqualToString:@"video"]) {
        CGFloat videoHeight = tuModel.videoModel.height.intValue * 394 / tuModel.videoModel.width.intValue;
        CGFloat ImageHeight = videoHeight;
        return rect.size.height + ImageHeight + 160;
    }if ([tuModel.type isEqualToString:@"image"]) {
        CGFloat imageHeight = tuModel.imageModel.height.intValue * 394 / tuModel.imageModel.width.intValue;
        
        CGFloat ImageHeight = imageHeight;
        return rect.size.height + ImageHeight + 160;
    }
    return 0;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DSLog(@"Cell点击方法");
    CommentsViewController *commentsVC = [[CommentsViewController alloc] init];
    TupianModel *model = self.allArray[indexPath.row];
    commentsVC.myID = model.ID;
    commentsVC.myVideo = model.videoModel.thumbnail[0];
    commentsVC.myGif = model.gifModel.images[0];
    commentsVC.myImage = model.imageModel.big[0];
    commentsVC.myType = model.type;
    commentsVC.myUrl = model.videoModel.video[0];
    commentsVC.myText = model.text;
    [self.navigationController pushViewController:commentsVC animated:YES];
}

@end
